// news-detail/news-detail.ts
import ColorUISdk from '../../mp-sdk/index'

const sdk = new ColorUISdk({
  env: 'dev',
  version: '1.0.0',
  api: {
    dev: { 
      // url: 'http://172.20.10.2:5000'
      url:'http://192.168.134.31:5000'
    }
  }
})

Page({
  data: {
    newsDetail: null as any,
    loading: true
  },

  onLoad(options: { id: string }) {
    const newsId = options.id
    this.loadNewsDetail(newsId)
  },

  // 返回按钮事件
  onBackTap() {
    wx.navigateBack({
      delta: 1
    })
  },

  async loadNewsDetail(uniquekey: string) {
    try {
      this.setData({ loading: true })
      const res = await sdk.request({
        url: '/api/news/detail',
        method: 'GET',
        data: { uniquekey }
      })
      
      if (res.code === 0) {
        this.setData({
          newsDetail: res.data
        })
      } else {
        wx.showToast({
          title: res.msg || '获取详情失败',
          icon: 'none'
        })
      }
    } catch (error) {
      console.error('加载新闻详情失败:', error)
      wx.showToast({
        title: '网络错误',
        icon: 'none'
      })
    } finally {
      this.setData({ loading: false })
    }
  }
})